<?php

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';
require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
require_once ROOT_PATH . '/lib/common/SearchObject.php';
require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';



class SkillUser {

	const TABLE_NAME = 'hs_hr_skill_user';
	
	const DB_FIELD_SKILL_USER_ID = 'ID';
	const DB_FIELD_SKILL_USER_EXPERTISE_ID = 'ExpertiseID';	
	const DB_FIELD_SKILL_USER_USER_ID = 'UserID';
	const DB_FIELD_SKILL_USER_VALUE = 'Value';
	const DB_FIELD_SKILL_USER_DATE_SUBMIT = 'DateSubmit';
	
	private $skill_user_id;
	private $skill_user_expertise_id;
	private $skill_user_user_id;
	private $skill_user_value;
	private $skill_user_date_submit;
	
	
	public function __construct($id = null) {
		$this->skill_user_id = $id;
	}

	public function setId($id) {
		$this->skill_user_id = $id;
	}
	
	public function getId() {
		return $this->skill_user_id;
	}
	
	public function setSkillUserExpertiseID($expertiseID) {
		$this->skill_user_expertise_id = $expertiseID;
	}
	
	public function getSkillUserExpertiseID() {
		return $this->skill_user_expertise_id;
	}
	
	public function setSkillUserUserID($userID){
		$this->skill_user_user_id = $userID;
	}
	
	public function getSkillUserUserID(){
		return $this->skill_user_user_id;
	}
	
	public function setSkillUserValue($value){
		$this->skill_user_value = $value;
	}
	
	public function getSkillUserValue(){
		return $this->skill_user_value;
	}
	
	public function setSkillUserDateSubmit($date){
		$this->skill_user_date_submit = $date;
	}
	
	public function getSkillUserDateSubmit(){
		return $this->skill_user_date_submit;
	}

	

    public function save() {
		if (isset($this->skill_user_id)) {
			return $this->_update();
		} else {
			return $this->_insert();
		}
    }

	public static function delete($ids) {

		$count = 0;
		if (!is_array($ids)) {
			throw new SkillUserException("Invalid parameter to delete(): ids should be an array", SkillUserException::INVALID_PARAMETER);
		}		
		return $count;
	}	

	public static function getAll() {
		return self::_getList();
	}
	
	public static function getSkillUserByExpertise($expertiseId,$userID) {
		$selectCondition[0] =  self::DB_FIELD_SKILL_USER_EXPERTISE_ID." = $expertiseId"; 
		$selectCondition[1] =  self::DB_FIELD_SKILL_USER_USER_ID." = $userID";
		//$selectCondition[2] =  self::DB_FIELD_SKILL_USER_VALUE." = $value";
		return self::_getList($selectCondition);
	}
	
	
	
	public static function getTotalSumaryByExpertise($expertiseId){
		$selectCondition[0] =  self::DB_FIELD_SKILL_USER_EXPERTISE_ID." = $expertiseId";
		$list = self::_getList($selectCondition);
		$intro = $basic = $inter = $adv = $exp = 0; 
		for($i= 0; $i <count($list);$i++){
			$value =$list[$i]->getSkillUserValue();
			$value==1?$intro++:$value==2?$basic++:$value==3?$inter++:$value==4?$adv++:$value==5?$exp++:null;
		}
		
		$result =  array($intro,$basic,$inter,$adv,$exp);
		//print_r($result);
		//die();
		return $result;
	}
	
	public static function getBasic($expertiseId){
		$selectCondition[0] =  self::DB_FIELD_SKILL_USER_EXPERTISE_ID." = $expertiseId";
		$selectCondition[1] =  self::DB_FIELD_SKILL_USER_VALUE." = 2";
		
			return count(self::_getList($selectCondition));	
	}
	public static function getIntermediate($expertiseId){
		$selectCondition[0] =  self::DB_FIELD_SKILL_USER_EXPERTISE_ID." = $expertiseId";
		$selectCondition[1] =  self::DB_FIELD_SKILL_USER_VALUE." = 3";
		return count(self::_getList($selectCondition));
	}
	public static function getAdvanced($expertiseId){
		$selectCondition[0] =  self::DB_FIELD_SKILL_USER_EXPERTISE_ID." = $expertiseId";
		$selectCondition[1] =  self::DB_FIELD_SKILL_USER_VALUE." = 4";
		return count(self::_getList($selectCondition));
	}
	public static function getExpertise($expertiseId){
		$selectCondition[0] =  self::DB_FIELD_SKILL_USER_EXPERTISE_ID." = $expertiseId";
		$selectCondition[1] =  self::DB_FIELD_SKILL_USER_VALUE." = 5";
		return count(self::_getList($selectCondition));
	}

	private static function _getList($selectCondition = null,$selectOrderBy=null,$selectOrder=null, $selectLimit=null) {
		$fields[0] = self::DB_FIELD_SKILL_USER_ID;
		$fields[1] = self::DB_FIELD_SKILL_USER_EXPERTISE_ID;
		$fields[2] = self::DB_FIELD_SKILL_USER_USER_ID;
		$fields[3] = self::DB_FIELD_SKILL_USER_VALUE;
		$fields[4] = self::DB_FIELD_SKILL_USER_DATE_SUBMIT;					
		
		$tables[0] = self::TABLE_NAME ;
		
		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->simpleSelect($tables[0],$fields,$selectCondition,$selectOrderBy,$selectOrder, $selectLimit);
		//die($sql);
		$actList = array();

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		while ($result && ($row = mysql_fetch_assoc($result))) {
			$actList[] = self::_createFromRow($row);
		}
		return $actList;
	}
	
	private static function _getList2($selectCondition = null,$selectOrderBy=null,$selectOrder=null, $selectLimit=null) {
		$fields[0] = self::DB_FIELD_SKILL_USER_ID;
		$fields[1] = self::DB_FIELD_SKILL_USER_EXPERTISE_ID;
		$fields[2] = self::DB_FIELD_SKILL_USER_USER_ID;
		$fields[3] = self::DB_FIELD_SKILL_USER_VALUE;
		$fields[4] = self::DB_FIELD_SKILL_USER_DATE_SUBMIT;					
		
		$tables[0] = self::TABLE_NAME ;
		
		$sqlBuilder = new SQLQBuilder();
		$sql = $sqlBuilder->simpleSelect($tables[0],$fields,$selectCondition,$selectOrderBy,$selectOrder, $selectLimit);
		die($sql);
		$actList = array();

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		while ($result && ($row = mysql_fetch_assoc($result))) {
			$actList[] = self::_createFromRow($row);
		}
		return $actList;
	}

	private function _insert() {
				
		//$fields[0] = self::DB_FIELD_SURVEY_ID;
		$fields[0] = self::DB_FIELD_SKILL_USER_EXPERTISE_ID;
		$fields[1] = self::DB_FIELD_SKILL_USER_USER_ID;
		$fields[2] = self::DB_FIELD_SKILL_USER_VALUE;
		$fields[3] = self::DB_FIELD_SKILL_USER_DATE_SUBMIT;
		

		//$values[0] = $this->survey_id;
		$values[0] = $this->skill_user_expertise_id;
		$values[1] = $this->skill_user_user_id;		
		$values[2] = $this->skill_user_value;
		$values[3] = $this->skill_user_date_submit?$this->skill_user_date_submit:'0000-00-00';		
		
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_insert = 'true';
		$sqlBuilder->arr_insert = $values;
		$sqlBuilder->arr_insertfield = $fields;

		$sql = $sqlBuilder->addNewRecordFeature2();

		$conn = new DMLFunctions();

		$result = $conn->executeQuery($sql);
		if (!$result || (mysql_affected_rows() != 1)) {
			throw new SkillUserException("Insert failed. ", SkillUserException::DB_ERROR);
		}

		return $this->skill_user_id;
	}

	private function _update() {

		$fields[0] = self::DB_FIELD_SKILL_USER_ID;
		$fields[1] = self::DB_FIELD_SKILL_USER_EXPERTISE_ID;
		$fields[2] = self::DB_FIELD_SKILL_USER_USER_ID;
		$fields[3] = self::DB_FIELD_SKILL_USER_VALUE;
		$fields[4] = self::DB_FIELD_SKILL_USER_DATE_SUBMIT;
		
		

		$values[0] = $this->skill_user_id;
		$values[1] = $this->skill_user_expertise_id;
		$values[2] = $this->skill_user_user_id;		
		$values[3] = $this->skill_user_value;
		$values[4] = $this->skill_user_date_submit?$this->skill_user_date_submit:'0000-00-00';				
				
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_update = 'true';
		$sqlBuilder->arr_update = $fields;
		$sqlBuilder->arr_updateRecList = $values;

		$sql = $sqlBuilder->addUpdateRecord1(0);
	
		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		if (!$result) {
			throw new SkillUserException("Update failed. SQL=$sql", SkillUserException::DB_ERROR);
		}
		return $this->skill_user_id;
	}
	
	

    private static function _createFromRow($row) {
    	
    	$skillUser = new SkillUser($row[self::DB_FIELD_SKILL_USER_ID]);
    	$skillUser->setId($row[self::DB_FIELD_SKILL_USER_ID]);
    	$skillUser->setSkillUserExpertiseID($row[self::DB_FIELD_SKILL_USER_EXPERTISE_ID]);
    	$skillUser->setSkillUserUserID($row[self::DB_FIELD_SKILL_USER_USER_ID]);
    	$skillUser->setSkillUserValue($row[self::DB_FIELD_SKILL_USER_VALUE]);
    	$skillUser->setSkillUserDateSubmit($row[self::DB_FIELD_SKILL_USER_DATE_SUBMIT]);
    	
    	return $skillUser;
    }
   

}

class SkillUserException extends Exception {
	const INVALID_PARAMETER = 0;
	const DB_ERROR = 1;
}
?>
